package org.example.java.util.concurrency.thread.condition;

/**
 * 与 SynchronizedTest3比较
 * @author juntao zhang
 */
public class SynchronizedTest5 {

    private synchronized void fun1() throws Exception {
            System.out.println(Thread.currentThread().getName() + " fun1 synchronized start");
            Thread.sleep(1000L);
            System.out.println(Thread.currentThread().getName() + " fun1 synchronized end");
    }

    private void fun2() throws Exception {
            System.out.println(Thread.currentThread().getName() + " fun2 synchronized start");
            Thread.sleep(1000L);
            System.out.println(Thread.currentThread().getName() + " fun2 synchronized end");
    }

    public static void main(String[] args) {
        //这4个方法是顺序执行的
        final SynchronizedTest5 test = new SynchronizedTest5();
        new Thread() {
            public void run() {
                try {
                    test.fun1();
                    test.fun2();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
        new Thread() {
            public void run() {
                try {
                    test.fun2();
                    test.fun1();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}
